﻿/**
 * @author catarina
 */
 import SCUMMCore.Menu;
 import SCUMMCore.Legenda;
 
class SCUMMCore.Item extends Object
{
	private var sNome:String;
	private var sLegenda:String;
	private var mc:MovieClip;
	private var oLegenda:Legenda;
	private var bSelecionado:Boolean;
	private var nXInicial:Number;
	private var nYInicial:Number;

	public function Item( pmcItem:MovieClip, psLegenda, psNome )
	{
		var thisObject:Item = this;
		this.bSelecionado = false;
		this.nXInicial = pmcItem._x;
		this.nYInicial = pmcItem._y;
		this.sLegenda = psLegenda;
		this.sNome = psNome;
		this.mc = pmcItem;
		this.mc.onRollOver = function()
		{
			thisObject.mostraLegenda();
		};
		this.mc.onPress = function()
		{
			thisObject.arrastaObjeto();
		};
		this.mc.onRelease = function()
		{
			thisObject.mostraTelaItem();
		};
		this.mc.onReleaseOutside = function()
		{
			thisObject.voltaPosicaoInicial();
		};
	}

	private function mostraTelaItem():Void
	{
		if ( !this.voltaPosicaoInicial() )
		{		
			if ( !this.bSelecionado )
			{
				var mcFundo:MovieClip = this.mc.attachMovie("ItemFundo", "Fundo", 0);
				mcFundo._width = this.mc._width;
				mcFundo._height = this.mc._height;
				this.mc.Imagem.swapDepths(this.mc.getNextHighestDepth());
			}
			else
			{
				this.mc.Fundo.removeMovieClip();
				this.mc["Fundo"].removeMovieClip();
	
				var mc:MovieClip =  _root.attachMovie(this.sNome + "Grande" , "mc" + this.sNome + "Grande", _root.getNextHighestDepth());
				mc._x = (_root._width - mc._width) / 2;
				mc._y = (_root._height - mc._height) / 2;
			}
			this.bSelecionado = !this.bSelecionado;
		}
	}
	
	private function mostraLegenda():Void
	{
		this.oLegenda = new Legenda( this.mc, this.sLegenda );
	}
	
	private function arrastaObjeto()
	{
		if ( !this.bSelecionado )
		{
			this.mc.startDrag();
		}
	}
	
	private function voltaPosicaoInicial():Boolean
	{
		this.mc.stopDrag();
		if ( this.mc._x != this.nXInicial || this.mc._y != this.nYInicial )
		{
			this.mc._x = this.nXInicial;
			this.mc._y = this.nYInicial;
			return true;
		}
		else
			return false;
	}
}